package com.example.zjsr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.zjsr.common.ServerResponse;
import com.example.zjsr.domain.Question;
import com.example.zjsr.domain.User;
import com.example.zjsr.service.QuestionService;
import com.example.zjsr.util.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实验答疑模块
 */
@Controller
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 添加问题
     * @param question
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addQuestion(@RequestBody Question question, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        question.setUsername(u.getUsername());
        question.setSchool(u.getSchool());
        return questionService.add(question);
    }

    /**
     * 默认分页显示所有的问题
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo<Question>> findAll(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject= JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questionList = questionService.findAll(u.getRoleId());
        PageInfo<Question> pageInfo = new PageInfo<>(questionList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 点击查看显示详情
     */
    @RequestMapping(value = "/findById",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Question> findByQuestionId(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=JSON.parseObject(data);
        Integer id=jsonObject.getInteger("id");
        return questionService.findById(id);
    }

    /**
     * 点击查看，根据问题id显示所有信息（评论+问题）
     */
    @RequestMapping(value = "/findAllById",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Question> findAllById(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=JSON.parseObject(data);
        Integer id=jsonObject.getInteger("id");
        return questionService.findAllById(id,u.getRoleId());
    }

    /**
     * 多参数查询问题 姓名、学校、实验问题 todo 分页功能 已完成！
     */
    @RequestMapping(value = "/findByParams",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo<Question>> findByParams( HttpServletRequest request, HttpServletResponse response,@RequestBody String data){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String username=jsonObject.getString("username");
        String school=jsonObject.getString("school");
        String question=jsonObject.getString("question");
        Question questions=new Question();
        questions.setUsername(username);
        questions.setSchool(school);
        questions.setQuestion(question);
        questions.setRoleId(u.getRoleId());
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Question> pageInfo = new PageInfo<>( questionService.findByParams(questions));
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 根据问题id删除问题
     */
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> deleteByQuestionId(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=JSON.parseObject(data);
        Integer id=jsonObject.getInteger("id");
        return questionService.deleteById(id);
    }

    /**
     * 根据问题id修改问题内容
     */
    @RequestMapping(value = "/updateById",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateById(@RequestBody Question question, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return questionService.updateById(question);
    }

    /**
     * 根据问题id审核问题内容
     */
    @RequestMapping(value = "/check",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> check(@RequestBody Question question, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            questionService.checkQuestion(question.getId());
            return ServerResponse.createBySuccessMsg("审核成功");
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("审核失败");
        }
    }
}
